/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.Cash;
import br.com.foxline.clinicplus.entity.ContaBancaria;
import br.com.foxline.clinicplus.entity.Income;
import br.com.foxline.clinicplus.enums.DiarySituation;
import br.com.foxline.clinicplus.enums.TypeDebt;
import br.com.foxline.clinicplus.enums.TypeIncome;
import br.com.foxline.clinicplus.service.CashService;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.IncomeService;
import br.com.foxline.clinicplus.service.ProcedureService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerFinancialOutput implements Serializable {

    @EJB
    IncomeService incomeService;
    @EJB
    DoctorService doctorService;
    @EJB
    ProcedureService procedureService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    CashService cashService;
    private Income income;
    private List<Income> incomes;
    private BigDecimal total;
    private ContaBancaria contaBancaria;

    @PostConstruct
    public void init() {
        income = new Income();
        income.setTypeIncome(TypeIncome.D);
        incomes = new ArrayList<Income>();
        total = new BigDecimal(0);
    }

    public List<SelectItem> selectTypeOutput() {
        List<SelectItem> list = new ArrayList<SelectItem>();

        for (TypeDebt ms : TypeDebt.values()) {
            list.add(new SelectItem(ms, ms.getDesc()));
        }
        return list;
    }

    public Income getIncome() {
        return income;
    }

    public void setIncome(Income income) {
        this.income = income;
    }

    public List<Income> getIncomes() {
        return incomes;
    }

    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    public void add() {
        try {
            income.setAmountPaid(income.getTotalAmount());
            total = total.add(income.getTotalAmount());

            if (income.getCreateAt() != null) {
                Calendar gc1 = GregorianCalendar.getInstance();
                gc1.setTime(income.getCreateAt());
                gc1.set(GregorianCalendar.HOUR_OF_DAY, 12);
                income.setCreateAt(gc1.getTime());
            }

            Cash cash = new Cash();
            cash.setValue(income.getTotalAmount().multiply(new BigDecimal("-1")));
            if (income.getCreateAt() != null) {
                cash.setCreatAt(new Date());
            } else {
                cash.setCreatAt(income.getCreateAt());
            }
            cash.setSystemUser(systemUserService.getCurrentUser());
            cash.setTypeIncome(TypeIncome.D);
            cash.setDescription(income.getTypeDebt().getDesc() + " - " + income.getDescricao());
            cash.setContaBancaria(contaBancaria);
            income.setCash(cash);


            income.setSystemUser(systemUserService.getCurrentUser());
            if (income.getCash() != null) {
                cashService.save(income.getCash());
            }
            incomeService.save(income);

            income = new Income();
            income.setTypeIncome(TypeIncome.D);
            
            Msg.messagemInfo("Cadastrado com Sucesso !");
        } catch (Exception e) {
            Msg.messagemError("Erro !");
        }


    }

    public void salvar() {
        if (incomes == null || incomes.isEmpty()) {
            Msg.messagemError("Adicione um elemento");
            return;
        }
        try {
            for (Income i : incomes) {
                i.setSystemUser(systemUserService.getCurrentUser());
                if (i.getCash() != null) {
                    cashService.save(i.getCash());
                }
                incomeService.save(i);
            }
            incomes = new ArrayList<Income>();
            total = BigDecimal.ZERO;
            Msg.messagemInfo("Cadastrado com Sucesso !");
        } catch (Exception e) {
            Msg.messagemError("Erro !");
        }

    }

    public BigDecimal getTotal() {
        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public ContaBancaria getContaBancaria() {
        return contaBancaria;
    }

    public void setContaBancaria(ContaBancaria contaBancaria) {
        this.contaBancaria = contaBancaria;
    }
}
